package com.iss.itemplate.dao.jpa;

import com.iss.itemplate.dao.GenericDao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.Serializable;

/**
 * **********************************************************************
 * <p/>
 * iServiceSoft Inc © - 2009 All Rights Reserved
 * _____________________________________________
 * <p/>
 * NOTICE:  All information contained herein is, and remains
 * the property of iServiceSoft. Intellectual and technical concepts
 * contained herein are proprietary of iServiceSoft and may be covered
 * by U.S. and Foreign Patents, patents in process, and are protected
 * by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from iServiceSoft Incorporated.
 * <p/>
 * ***********************************************************************
 */
public abstract class GenericJpaDao<T, ID extends Serializable> implements GenericDao<T, ID> {

   public static String LIKE_CHARACTER = "%";

   private final Class<T> persistentClass;

   private EntityManager entityManager;
   
   @PersistenceContext
   public void setEntityManager(EntityManager entityManager) {
	   this.entityManager = entityManager;
   }

   public EntityManager getEntityManager() {
      return entityManager;
   }

   public GenericJpaDao(final Class<T> persistentClass) {
      this.persistentClass = persistentClass;
   }

   @Override
   public T findById(ID id) {
      return entityManager.find(persistentClass, id);
   }

   @Override
   public void delete(ID id) {
      T deleteObject = entityManager.find(persistentClass, id);
      entityManager.remove(deleteObject);
   }

   @Override
   public void delete(T t) {
      entityManager.remove(t);
   }

   @Override
   public void insert(T t) {
      entityManager.persist(t);
   }

   @Override
   public void update(T t) {
      entityManager.merge(t);
   }
}
